package com.supermap.data.controller;

import com.supermap.data.modal.TUsers;
import com.supermap.data.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = {"/user","/data/user"})
public class UserController extends BaseController {

    @Autowired
    UserService userService;


    /**
     * 用户登录验证、获取用户信息
     *
     * @param request
     * 			http请求
     * @param response
     * 			http响应
     * @param userName
     * 			用户名
     * @param password
     * 			密码
     * @return 登录后的结果
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> login(HttpServletRequest request, HttpServletResponse response, String userName, String password) {
        Map<String, Object> re = new HashMap<>();

        // 验证并获取用户信息
        TUsers user = userService.checkUser(userName, password);

        if (user == null) {
            TUsers users2 = userService.findByUserName(userName);
            re.put("status", false);
            if (users2 != null){
                if (users2.getStatus() == 0){
                    re.put("info", "该用户已被停用!");
                }else {
                    re.put("info", "用户名或密码不正确!");
                }
            }else{
                re.put("info", "用户名或密码不正确!");
            }
            System.out.println(re);
            return re;
        }

        // 设置Session用户信息
        setSessionUser(request, user);
        re.put("status", true);
        re.put("info", "登录成功!");

        re.put("user", user);

        return re;
    }

}
